from PySide6.QtWidgets import (
    QApplication,
    QVBoxLayout,
    QHBoxLayout,
    QWidget,
    QPushButton,
    QLabel,
    QToolBar,
    QStyle,
    QStyleOption,
)
from PySide6.QtGui import (
    QAction,
    QIcon,
    QPixmap,
    QPainter,
    QCursor,
    QPaintEvent,
    QMouseEvent,
    QResizeEvent,
)
from PySide6.QtCore import Qt, QSize
from pages.shopCart import ShopCart
from pages.shopOrder import ShopOrder
from pages.shopwindow import ShopWindow
import os, sys, time

# from components.topSysTool import TopSysTool


class MainWindow(QWidget):
    def __init__(self, parent=None):
        super().__init__()
        favicon = QPixmap("title2.png").scaled(
            40, 40, Qt.KeepAspectRatio, Qt.SmoothTransformation
        )
        self.setWindowTitle("My App")
        self.setWindowIcon(QIcon(favicon))
        self.setWindowFlags(
            Qt.Window
            | Qt.FramelessWindowHint  # | Qt.SubWindow | Qt.WindowStaysOnBottomHint
        )
        self.setAttribute(Qt.WA_TranslucentBackground)
        self.m_flag = False
        # ----------------------------
        # 系统按钮栏
        # ----------------------------
        title_icon = QLabel()
        title_icon.setPixmap(favicon)

        title_text = QLabel()
        title_text.setText("WeApp")
        title_text.setStyleSheet("color: white; font-size: 16px; font-weight: bold;")

        toolbar = QToolBar()
        toolbar.setIconSize(QSize(24, 24))
        self.maxWindow = QAction(
            QApplication.style().standardIcon(QStyle.SP_TitleBarMaxButton),
            "最大化",
            self,
        )
        toolbar.addAction(
            QAction(
                QApplication.style().standardIcon(QStyle.SP_TitleBarMinButton),
                "最小化",
                self,
            )
        )
        toolbar.addAction(self.maxWindow)
        toolbar.addAction(
            QAction(
                QApplication.style().standardIcon(QStyle.SP_TitleBarCloseButton),
                "关闭",
                self,
            )
        )
        h0 = QHBoxLayout()
        h0.setContentsMargins(0, 0, 0, 0)
        h0.addWidget(title_icon)
        h0.addWidget(title_text)
        h0.addStretch(1)
        h0.addWidget(toolbar)

        # ----------------------------
        # 正文body栏
        # ----------------------------
        self.shopWindow = ShopWindow()
        h1 = QHBoxLayout()
        h1.setContentsMargins(5, 5, 5, 5)
        # h1.setStyleSheet("background-color: #E1E3FC; ")
        h1.addLayout(ShopCart())
        h1.addLayout(self.shopWindow)
        h1.addLayout(ShopOrder())

        # ----------------------------
        # 顶层布局
        # ----------------------------
        m = QVBoxLayout(self)
        m.setContentsMargins(0, 0, 0, 0)
        m.addLayout(h0)
        m.addLayout(h1)
        m.addStretch(1)

        # 信号与槽
        toolbar.actionTriggered.connect(self.MSG_SYSTEM)

        # 样式
        self.setStyleSheet(
            "MainWindow {color: rgba(255, 255, 255, 255); background-color: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:1, stop:0 rgba(14,70,200, 255), stop:1 rgba(255, 255, 255, 255)); border-radius: 5px;}"
        )

    def MSG_SYSTEM(self, event):
        """系统工具栏消息"""
        if event.text() == "关闭":
            QApplication.quit()
        elif event.text() == "最小化":
            self.showMinimized()
        elif event.text() == "最大化":
            self.maxWindow.setText("默认")
            self.maxWindow.setIcon(
                QApplication.style().standardIcon(QStyle.SP_TitleBarNormalButton)
            )
            self.showFullScreen()
            print("max:", self.size().height())
            self.shopWindow.reSizeContainer(self.size().height())
        elif event.text() == "默认":
            self.maxWindow.setText("最大化")
            self.maxWindow.setIcon(
                QApplication.style().standardIcon(QStyle.SP_TitleBarMaxButton)
            )
            self.showNormal()
            print("normal:", self.size().height())
            self.shopWindow.reSizeContainer(self.size().height())

    def paintEvent(self, event: QPaintEvent) -> None:
        opt = QStyleOption()
        opt.initFrom(self)
        p = QPainter(self)
        self.style().drawPrimitive(QStyle.PE_Widget, opt, p, self)
        return super().paintEvent(event)

    def resizeEvent(self, event: QResizeEvent):
        super().resizeEvent(event)
        # event.accept()
        # self.shopWindow.reSizeContainer(self.size().height())

    def mousePressEvent(self, event: QMouseEvent):
        if event.button() == Qt.LeftButton:
            if self.isMaximized() | self.isFullScreen():
                return
            pos = event.globalPosition().toPoint()
            self.m_flag = True
            self.m_Position = pos - self.pos()
            event.accept()
            self.setCursor(QCursor(Qt.OpenHandCursor))

    def mouseMoveEvent(self, event: QMouseEvent):
        if (event.buttons() == Qt.LeftButton) & (self.m_flag):
            pos = event.globalPosition().toPoint()
            self.move(pos - self.m_Position)
            event.accept()

    def mouseReleaseEvent(self, event: QMouseEvent):
        self.m_flag = False
        self.setCursor(QCursor(Qt.ArrowCursor))


if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = MainWindow()
    window.show()
    sys.exit(app.exec())
